package trabalhotec.BD;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Vector;


public class Promocao extends Voos {
    Vector<Vector> promo;
    Vector<Vector> promosexist;
    public void excluirPromo(String cod){
        con = null;
        stmt = null;
        try {
            con = Conexao.abrirConexao();
            stmt = con.createStatement();
            String excluir = "Delete from promocao where Codigo = '"+cod+"'";
            stmt.executeUpdate(excluir);
        } catch (SQLException ex) {
            System.out.println("Erro ao acessar banco de dados");
        }finally{
            try {
                stmt.close();
                con.close();
            } catch (SQLException ex) {
                System.out.println("Erro ao fechar conexao");
            }
        }
    }
    public boolean promosExistentes(String data){
        con = null;
        stmt = null;
        boolean val = true;
        try {
            con = Conexao.abrirConexao();
            stmt = con.createStatement();
            Timestamp d = DataUtil.obterDataParaBanco(data);
            String datas = "Select * from promocao where ('"+d+"' > datainicio AND datafim > '"+d+"')";
            ResultSet rs = stmt.executeQuery(datas);
            if(rs.next()) val= false;
        } catch (SQLException ex) {
            System.out.println("erro ao acessar Banco de dados");
        }finally{
            try {
                stmt.close();
                con.close();
            } catch (SQLException ex) {
                System.out.println("erro ao fechar conexao");
            }
        }
        return val;
    }
    public Vector<Vector> listarPromo(){
        try {
            promo = new Vector<Vector>();
            con = Conexao.abrirConexao();
            stmt = con.createStatement();
            String listar = "Select * from promocao";
            ResultSet rs = stmt.executeQuery(listar);
            while(rs.next()){
                Vector list = new Vector();
                list.add(rs.getString("Codigo"));
                list.add(DataUtil.obterDataDoBanco(rs.getTimestamp("Datainicio")));
                list.add(DataUtil.obterDataDoBanco(rs.getTimestamp("Datafim")));
                list.add(rs.getFloat("Desconto"));
                list.add(rs.getString("Voo"));
                promo.add(list);
            }
        } catch (SQLException ex) {
            System.out.println("erro ao acessar banco de dados");
        } finally{
            try{
                stmt.close();
                con.close();
            }catch (Exception e1){
                System.out.println("Erro ao tentar fechar conexao");

            }
        }
        return promo;
        
    }
    public void cadPromo(String Codigo, String dataini,String datafim, float desc, String codvoo){
        con = null;
        stmt = null;
        try {
            con = Conexao.abrirConexao();
            stmt = con.createStatement();
            Timestamp datainicial = DataUtil.obterDataParaBanco(dataini);
            Timestamp datafinal = DataUtil.obterDataParaBanco(datafim);
            String inserir = "Insert into promocao values ('"+Codigo+"','"+datainicial+"','"+datafinal+"',"+desc+",'"+codvoo+"')";
            stmt.executeUpdate(inserir);
        } catch (SQLException ex) {
            System.out.println("Erro ao acessar o banco de dados");
           
        }finally{
            try {
                stmt.close();
                con.close();
            } catch (SQLException ex) {
                System.out.println("Erro ao fechar conexao com o banco de dados");
            }
        }
    }
}
